package de.hauck.rtmidlet.utils;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import de.hauck.rtmidlet.model.Configuration;

public class WorkerThread extends Thread {

	// private String _path;

	private boolean mCancel;
	private boolean mTrucking = true;
	private String _message;
	private FileConnection logConnection = null;
	private String fullPath;
	private DataOutputStream streamOut = null;
	public WorkerThread() {
		// mMIDlet = midlet;
		// _path=path;
		mCancel = false;
		String LogFileName = "RTMidlet.txt";
		System.out.println("export path: "+Configuration.exportPath);
		if (null!=Configuration.exportPath && !Configuration.exportPath.equals("")) {
		fullPath = "file:///" + Configuration.exportPath + LogFileName;
		

		try {
			// ------------------------------------------------------------------
			// Create a FileConnection and if this is a new stream create
			// the
			// file
			// ------------------------------------------------------------------

			// Logger.debug("FILE: path is " + fullPath);
			if (logConnection == null) {
				try {
					logConnection = (FileConnection) Connector.open(fullPath);
					// Create file
					if (logConnection != null && !logConnection.exists()) {
						logConnection.create();
					} else {
						// File already exists: zero the contents so we get
						// a new log each time
						// logConnection.delete();
						logConnection.truncate(0);
					}
				} catch (IOException e) {
					Configuration.fileWritingPossible = false;
					MyLogger.debug("WriteLog failed to create connection:"
							+ fullPath + ":" + e.getMessage());
					logConnection = null;
				}
			}

			if (logConnection != null && streamOut == null) { // XXX:
				// streamOut
				// is always
				// null
				// here?
				// open the stream at the end so we can append to the file
				System.out.println("Filesize:" + logConnection.fileSize());
				OutputStream x = logConnection.openOutputStream(logConnection
						.fileSize());
				streamOut = new DataOutputStream(x);
			}
		} catch (IOException e) {
			Configuration.fileWritingPossible = false;
			System.out.println("Logger: error:" + e.getMessage()+" - disabling FS-Log");
			e.printStackTrace();
		}
		}

	}

	public synchronized void run() {
		while (mTrucking) {
			try {
				wait();
			} catch (InterruptedException ie) {
			}
			if (mTrucking)
				logMessage(_message);
		}
	}

	public synchronized void go(String message) {
		_message = message;
		notify();
	}

	public void cancel() {
		try {
			mCancel = true;
			if (logConnection != null)
				logConnection.close();
			if (null!=streamOut) streamOut.close();
		} catch (IOException ignored) {
		}
	}

	private void logMessage(String message) {
			if (streamOut != null) {
				try {
					
					streamOut.write(((String) _message + "\n").getBytes());
					streamOut.flush();
					//streamOut.close();
					//streamOut = null;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				Configuration.fileWritingPossible = false;
				System.out.println("Logger: output stream is null - disabling FS-Log");
			}
	}

}
